local GoodsData = require("app.datacenter.GoodsData")
--交易商店
local DyTradeStore = {
    storelist1 = {},
    storelist2 = {},
    storelist3 = {},

    goods2ConstA = {},--货物常数A

    taxs1 = {},--千分制  40/1000
    taxs2 = {},
    taxs3 = {},
    camps = {},

    bags = {},
    tradingItem = nil,
    tradingMsg = {} --实时交易信息
}

--param goodstype:第几类货物
function DyTradeStore:initCityBuy()
    for k,cur_city_buy in pairs(configData.cfg_city_buy) do
        local itemlist = string.split(cur_city_buy["goods1"],"#")
        self.storelist1[cur_city_buy.id] = {}
        for i=1,#itemlist do
            local id = tonumber(itemlist[i])
            local item = GoodsData.new(id,-1)--无限
            table.insert(self.storelist1[cur_city_buy.id],item)
        end
        local itemlist2 = string.split(cur_city_buy["goods2"],"#")
        local goods2change = string.split(cur_city_buy["goods2change"],"&")
        for i=1,#itemlist2 do
            local id = tonumber(itemlist2[i])
            self.goods2ConstA[id] = tonumber( string.split(goods2change[i],"#")[1] )
        end
    end
end
--获取第X类货物的列表
function DyTradeStore:getStoreList()
    if self.goodstype==GOOODS_TYPE_LOCAL then
        return self.storelist1[RoleData.player_info.stageid]
    elseif self.goodstype==GOOODS_TYPE_LIVE then
        return self.storelist2[RoleData.player_info.stageid] or {}
    elseif self.goodstype==GOOODS_TYPE_EMERGENCY then
        return self.storelist3
    end
end

function DyTradeStore:getTax(goodstype)
    goodstype = goodstype or self.goodstype
    if goodstype==GOOODS_TYPE_LOCAL then
        return self.taxs1[RoleData.player_info.stageid]/10
    elseif goodstype==GOOODS_TYPE_LIVE then
        return self.taxs2[RoleData.player_info.stageid]/10
    elseif goodstype==GOOODS_TYPE_EMERGENCY then
        return self.taxs3[RoleData.player_info.stageid]/10
    end
end

function DyTradeStore:setGoodsStore(msg)
    for stageid,data in pairs(msg) do
        local id = tonumber(stageid)
        self.taxs1[id] = data.tax1 or self.taxs1[id]
        self.taxs2[id] = data.tax2 or self.taxs2[id]
        self.taxs3[id] = data.tax3 or self.taxs3[id]
        self.camps[id] = data.camp
        self.storelist2[id] = {}
        for goods2id,goods2value in pairs(data.goods2store or {}) do
            local item = GoodsData.new(tonumber(goods2id),goods2value.store)
            table.insert(self.storelist2[id],item)
        end
    end
end

function DyTradeStore:purchaseGoods(itemid,itemcount)
    local goodstype = configData.cfg_items[itemid].quality
    if goodstype==GOOODS_TYPE_LOCAL then
        -- 一类货物无限
        return
    elseif goodstype == GOOODS_TYPE_LIVE then
        local list = self:getStoreList()
        for id,item in pairs(list) do
            if item.id==itemid then
                item:setCount(item:getCount()-itemcount)
                return item:getCount()>=0
            end
        end
        return false
    end
end

function DyTradeStore:undoPurchase(itemid,itemcount)
    local goodstype = configData.cfg_items[itemid].quality
    if goodstype==GOOODS_TYPE_LOCAL then
        -- 一类货物无限
        return
    elseif goodstype == GOOODS_TYPE_LIVE then
        local list = self:getStoreList()
        for id,item in pairs(list) do
            if item.id==itemid then
                item:setCount(item:getCount()+itemcount)
                return item:getCount()>=0
            end
        end
        return false
    end
end

function DyTradeStore:getGoods2ConstA(id)
    return self.goods2ConstA[id]
end

function DyTradeStore:getGoods2Store(id)
    local list = self.storelist2[RoleData.player_info.stageid]
    for k,v in pairs(list) do
        if v.id == id then
            return v:getCount()
        end
    end
    return 0
end

function DyTradeStore:setGoodsType(goodstype)
    self.goodstype = goodstype
end

function DyTradeStore:reset()
    self.storelist1 = {}
    self.storelist2 = {}
    self.storelist3 = {}
end

function DyTradeStore:addTradingMsg(tradeData)
    table.insert(self.tradingMsg,tradeData)
end

function DyTradeStore:getTradingMsg()
    return self.tradingMsg
end

function DyTradeStore:checkTradingMsg()
    return next(self.tradingMsg) ~= nil
end

function DyTradeStore:popTradingMsg()
    local index,tradeData = next(self.tradingMsg)
    self.tradingMsg[index] = nil
    return tradeData or false
end

return DyTradeStore